package com.eostoken.eos_wallet.mvp.mode;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.eostoken.eos_wallet.bean.BillBean;
import com.eostoken.eos_wallet.bean.TransactionDetailsBean;
import com.eostoken.eos_wallet.eosnet.common.EOSNetWorkManager;
import com.eostoken.eos_wallet.eosnet.common.Params;
import com.eostoken.eos_wallet.mvp.callback.CommonCallback;
import com.eostoken.eos_wallet.net.common.RxSubscriber;
import com.eostoken.eos_wallet.utils.Constants;
import com.fredy.mvp.Model;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.eostoken.eos_wallet.utils.Constants.BILL;
import static com.eostoken.eos_wallet.utils.Constants.TRANSACTION;

public class BillModel implements Model {

    public void getActions(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getActions(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getActions onFail: " + failMsg);
                        callback.onFailure(failMsg, BILL);
                    }

                    @Override
                    public void onSuccess(String json) {
                        BillBean billBean = null;
                        try {
                            billBean = GsonUtils.fromJson(json, BillBean.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.dTag(Constants.TAG_MGP, "getActions onSuccess: " + json);
                        callback.onSuccess(billBean, "", BILL);
                    }
                });
    }

    public void getTransaction(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getTransaction(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getTransaction onFail: " + failMsg);
                        callback.onFailure(failMsg, TRANSACTION);
                    }

                    @Override
                    public void onSuccess(String json) {
                        LogUtils.dTag(Constants.TAG_MGP, "getTransaction onSuccess: " + json);
                        TransactionDetailsBean detailsBean = GsonUtils.fromJson(json, TransactionDetailsBean.class);
                        callback.onSuccess(detailsBean, "", TRANSACTION);
                    }
                });
    }
}
